<?php
/**
 * 文件上传类
 */

class upload{
    public $err = '';
     function add($appId,$file_name){
        // 设置上传目录
        $uploadDir = "uploads/App/{$appId}/";
        $uplpadPath = APP.$uploadDir;

        // 确保上传目录存在
        if (!is_dir($uplpadPath)) {
            mkdirs($uplpadPath, 0777);
        }
// 检查是否有文件上传
//         var_dump($_FILES);exit;

        if (isset($_FILES[$file_name])) {
            $file = $_FILES[$file_name];

            // 获取文件信息
//            $fileName =iconv("UTF-8", "GBK//IGNORE",  $file['name']);;
            $fileName=$file['name'];
                $fileType = $file['type'];
            $fileSize = $file['size'];
            $fileTmpName = $file['tmp_name'];
            $fileError = $file['error'];

            // 检查上传过程中是否发生错误
            if ($fileError === UPLOAD_ERR_OK) {
                // 生成新的文件名（可选，为了避免文件名冲突）
//                $newFileName = uniqid() . '_' . basename($fileName);
                $newFileName =date('Ymd_H时i分s'). '_' . basename($fileName);
//                echo $fileName;exit;

                // 完整的文件路径
                $targetFilePath = $uplpadPath . iconv("UTF-8", "GBK//IGNORE", $newFileName);
                // 将临时文件移动到目标路径
                    if (move_uploaded_file($fileTmpName, $targetFilePath)) {
                        return get_host().'/'.$uploadDir.$newFileName;
                    } else {
                        $this->err = "文件写入失败，file：{$targetFilePath}";
                    }
            } else {
                $this->err = '文件上传出错：'.$fileError;
            }
        } else {
            $this->err = '没有文件被上传：';
        }
        return false;
    }

    function getErr(){
         return $this->err;
    }


}